package him.action;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tiles.TilesContainer;
import org.apache.tiles.servlet.context.ServletUtil;

public class ActionForward {
	private String url;
	private boolean isTiles=true;
	private boolean isRedirect=false;
	
	public ActionForward(){
		isRedirect = false;
		url="";
	}
	public ActionForward(String url, boolean isRed){
		this.url=url;
		this.isRedirect = isRed;
	}
	public boolean isTiles(){
		return isTiles;
	}
	public void setTiles(boolean isTiles) {
		this.isTiles = isTiles;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public boolean isRedirect() {
		return isRedirect;
	}
	public void setRedirect(boolean isRedirect) {
		this.isRedirect = isRedirect;
	}
	
	public void go(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		if (isTiles) {
			tiles(getUrl(),request,response);
		}else if (isRedirect) {
			redirect(getUrl(),request,response);
		}else {
			dispatch(getUrl(),request,response);
		}
	}
	private void dispatch(String url2, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatch = request.getRequestDispatcher(url2);
		dispatch.forward(request, response);
	}
	private void redirect(String url2, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.sendRedirect(url2);
	}
	private void tiles(String url2, HttpServletRequest request,HttpServletResponse response) {
		System.out.println(request.getSession().getServletContext());
		System.out.println(ServletUtil.getContainer(request.getSession().getServletContext()));
		TilesContainer container = ServletUtil.getContainer(request.getSession().getServletContext());
		container.render(url2, request,response);
	}
}
